﻿using System;
using System.Data;
using System.Collections.Generic;
using LTP.Common;
using TCS.Model;
namespace TCS.BLL
{
    /// <summary>
    /// 业务逻辑类Gsp_SalesInfo 的摘要说明。
    /// </summary>
    public class Gsp_SalesInfo
    {
        private readonly TCS.DAL.Gsp_SalesInfo dal = new TCS.DAL.Gsp_SalesInfo();
        public Gsp_SalesInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int CGID)
        {
            return dal.Exists(CGID);
        }

        /// <summary>
        /// 增加一条销售信息数据
        /// </summary>
        public int Add(TCS.Model.Gsp_SalesInfo model)
        {
            try
            {
                return dal.Add(model);
                //int DrugID = model.DrugID == null ? 0 : Convert.ToInt32(model.DrugID);
                //int SalesCount = model.SalesCount == null ? 0 : Convert.ToInt32(model.SalesCount);
                
                //if (new TCS.DAL.Gsp_ProcurementInfo().updateCount(DrugID, model.BatchNumber, SalesCount))
                //{
                //    return dal.Add(model);
                //}
                //else
                //{
                //    throw new Exception("更新失败！");
                //}
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(TCS.Model.Gsp_SalesInfo model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int CGID)
        {

            dal.Delete(CGID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TCS.Model.Gsp_SalesInfo GetModel(int CGID)
        {

            return dal.GetModel(CGID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public TCS.Model.Gsp_SalesInfo GetModelByCache(int CGID)
        {

            string CacheKey = "Gsp_SalesInfoModel-" + CGID;
            object objModel = LTP.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(CGID);
                    if (objModel != null)
                    {
                        int ModelCache = LTP.Common.ConfigHelper.GetConfigInt("ModelCache");
                        LTP.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (TCS.Model.Gsp_SalesInfo)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<TCS.Model.Gsp_SalesInfo> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<TCS.Model.Gsp_SalesInfo> DataTableToList(DataTable dt)
        {
            List<TCS.Model.Gsp_SalesInfo> modelList = new List<TCS.Model.Gsp_SalesInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                TCS.Model.Gsp_SalesInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new TCS.Model.Gsp_SalesInfo();
                    if (dt.Rows[n]["CGID"].ToString() != "")
                    {
                        model.CGID = int.Parse(dt.Rows[n]["CGID"].ToString());
                    }
                    if (dt.Rows[n]["MemberID"].ToString() != "")
                    {
                        model.MemberID = int.Parse(dt.Rows[n]["MemberID"].ToString());
                    }
                    if (dt.Rows[n]["DrugID"].ToString() != "")
                    {
                        model.DrugID = int.Parse(dt.Rows[n]["DrugID"].ToString());
                    }
                    model.BatchNumber = dt.Rows[n]["BatchNumber"].ToString();
                    model.Period = dt.Rows[n]["Period"].ToString();
                    model.PurchaseUnit = dt.Rows[n]["PurchaseUnit"].ToString();
                    if (dt.Rows[n]["SalesCount"].ToString() != "")
                    {
                        model.SalesCount = int.Parse(dt.Rows[n]["SalesCount"].ToString());
                    }
                    model.SalesTime = dt.Rows[n]["SalesTime"].ToString();
                    model.Managor = dt.Rows[n]["Managor"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}
        public DataSet GetStatisticsList(string strWhere)
        {
            return dal.GetStatisticsList(strWhere);
        }
        #endregion  成员方法
    }
}

